package cn.afactory.tygz.model;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.common.ErrorCode;

/**
 * Created with Android Studio.
 * Title:UserInfoList
 * Description:
 * Copyright:Copyright (c) 2015
 * Company:和平必胜、正义必胜、人民必胜
 * Author:炎黄子孙
 * Date:15/9/19
 * Time:下午8:30
 * Version 1.0
 */
public class ScenicSpotsInfoList extends BaseDataHeader implements Serializable{

    private static final long serialVersionUID = -5096696705566385057L;
    public int count ;

    public List<ScenicSpotsInfo> value;

    public static ScenicSpotsInfoList deserialize(String json) throws JSONException {
        if (json != null && !json.isEmpty()) {
            return deserialize(new JSONObject(json));
        }
        return null;
    }

    public static ScenicSpotsInfoList deserialize(JSONObject json) throws JSONException {
        if (json != null && json != JSONObject.NULL && json.length() > 0) {
            ScenicSpotsInfoList result = new ScenicSpotsInfoList();
            result.message = json.optString("message");
            result.state = json.optInt("state");
            json = json.optJSONObject("data");
            if(result.state == ErrorCode.STATUS_OK) {
                if (json != null) {
                    result.count = json.optInt("count");
                    JSONArray valueJson = json.optJSONArray("value");
                    if (valueJson != null) {
                        int len = valueJson.length();
                        result.value = new ArrayList<ScenicSpotsInfo>(len);
                        for (int i = 0; i < len; i++) {
                            JSONObject jo = valueJson.optJSONObject(i);
                            if (jo != null && jo != JSONObject.NULL) {
                                result.value.add(ScenicSpotsInfo.deserialize(jo));
                            }
                        }
                    }
                }
            }
            return result;
        }
        return null;
    }
}
